package com.block.im.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.block.db.entity.ImUserBaseInfo;
import com.block.db.mapper.ImUserBaseInfoMapper;
import com.block.im.enums.UserTypeEnum;
import com.block.im.service.IImUserBaseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-04-17
 */
@Service
public class ImUserBaseInfoServiceImpl extends ServiceImpl<ImUserBaseInfoMapper, ImUserBaseInfo> implements IImUserBaseInfoService {

    @Override
    public ImUserBaseInfo getEntityByAccountIdAndType(String accountId, UserTypeEnum userTypeEnum) {

        String userId = StrUtil.format("{}:{}", userTypeEnum.getValue(), accountId);
        ImUserBaseInfo userBaseInfo = this.getById(userId);
        if (ObjectUtil.isEmpty(userBaseInfo)){

            userBaseInfo = new ImUserBaseInfo();
            userBaseInfo.setUserId(userId);
            userBaseInfo.setAccountId(accountId);
            userBaseInfo.setAccountType(userTypeEnum.getValue());

            // TODO: 2024/4/17 根据用户类型，生成不同的用户的信息
            userBaseInfo.setNickname("");
            userBaseInfo.setHeadUrl("");

            boolean save = this.save(userBaseInfo);
            if (!save){
                return null;
            }
            userBaseInfo = this.getById(userId);
        }
        return userBaseInfo;
    }
}
